let map = new BMap.Map("container");
map.addControl(new BMap.NavigationControl());               // 添加平移缩放控件
map.addControl(new BMap.ScaleControl());                    // 添加比例尺控件
map.addControl(new BMap.OverviewMapControl());              //添加缩略地图控件
map.addControl(new BMap.MapTypeControl({
    mapTypes:[
        BMAP_NORMAL_MAP,
        BMAP_HYBRID_MAP
    ]}));

//三种驾车策略：最少时间，最短距离，避开高速
let routePolicy = [BMAP_DRIVING_POLICY_LEAST_TIME, BMAP_DRIVING_POLICY_LEAST_DISTANCE, BMAP_DRIVING_POLICY_AVOID_HIGHWAYS];
let line_colors = ["#FF9900", "#2BAADF", "#BD2D30", "#0273DD", "#5CB85C", "#A479E2", "#D94235", "#E9B330", "#FE7300", "#3385FF", "#FF555D", "#19A97B"];
let color_index = 0;
function get_color(){
    if(color_index >= line_colors.length){
	color_index = 0;
    }
    
    return line_colors[color_index++];
}

// var myP1 = new BMap.Point(106.521436,29.532288);    //起点-重庆
// var myP2 = new BMap.Point(108.983569,34.285675);    //终点-西安
// var myP3 = new BMap.Point(116.404449,39.920423);    //终点-北京

let p_list = [ new BMap.Point(103.935501,30.756425),
	       new BMap.Point(104.088429,30.744011),
	       new BMap.Point(104.14707,30.636187),
	       new BMap.Point(104.130398,30.570539),
	       new BMap.Point(104.361514,30.675455),
	       new BMap.Point(104.356915,30.530232)];

map.clearOverlays();
function search(points, policy){
    let driving = new BMap.DrivingRoute(map, {
	policy:policy
    });
    if(points.length == 2){
	driving.search(points[0], points[1]);
    }else if(points.length > 2){
	for(let i = 0; i < points.length - 1; i++){
	   driving.search(points[i], points[i+1]);
	}
    }

    /*
    for(let i = 0; i < points.length; i++){
	//let m = new BMap.Marker(points[i]);
	//map.addOverlay(m);
	
	if(i == 0){
	    let lab1 = new BMap.Label("起点",{position:points[i]});
	     lab1.setStyle({color:"blue"});
	    map.addOverlay(lab1);
	}else if(i == points.length - 1){
	    //let lab1 = new BMap.Label("终点",{position:points[i]});
	    //lab1.setStyle({color:"red"});
	   // map.addOverlay(lab1);
	}else{
	    let lab1 = new BMap.Label("途径点",{position:points[i]});
	    map.addOverlay(lab1);
	}
	
    }
    */
    driving.setSearchCompleteCallback(function(){
	let results = driving.getResults();

	for(let i = 0; i < results.getNumPlans(); i++){
	    let plan1 = results.getPlan(i);
	    let routeCount = plan1.getNumRoutes();
	    for(let j = 0; j < routeCount; j++){
		let pts = plan1.getRoute(j).getPath();    //通过驾车实例，获得一系列点的数组
	     
		let polyline = new BMap.Polyline(pts, {
		    strokeColor: "#0F96FF",
		    strokeWeight: 2,
		    strokeOpacity:1
		});
	     
		map.addOverlay(polyline);
	    }
	    
	}
	
    });

    return driving;
}

for(let i = 0; i < end_points.length; i++){
    let s_point = start_points[i];
    let e_point = end_points[i];
    let t_points = [new BMap.Point(s_point[0], s_point[1]),new BMap.Point(e_point[0], e_point[1])];
    //let curve = new BMapLib.CurveLine(t_points, {strokeColor:"blue", strokeWeight:3, strokeOpacity:0.5}); //创建弧线对象
    //map.addOverlay(curve); //添加到地图中
    search(t_points, routePolicy[0]);
}
//search(p_list, routePolicy[0]);
//search(p_list, routePolicy[1]);
//search(p_list, routePolicy[2]);

// map.centerAndZoom(new BMap.Point(116.404, 39.915), 14);    
   
// driving.search(myP1, myP2);                 //第一个驾车搜索
// driving.search(myP2, myP3);                 //第二个驾车搜索

// var m1 = new BMap.Marker(myP1);         //创建3个marker
// var m2 = new BMap.Marker(myP2);
// var m3 = new BMap.Marker(myP3);
// map.addOverlay(m1);
// map.addOverlay(m2);
// map.addOverlay(m3);
         
// var lab1 = new BMap.Label("起点",{position:myP1});        //创建3个label
// var lab2 = new BMap.Label("途径点",{position:myP2});
// var lab3 = new BMap.Label("终点",{position:myP3});   
// map.addOverlay(lab1);
// map.addOverlay(lab2);
// map.addOverlay(lab3);
//*
setTimeout(function(){
    map.setViewport([new BMap.Point(102.714601, 25.049153), new BMap.Point(104.833852, 27.568916)]);          //调整到最佳视野
},1000);   

//*/
map.enableScrollWheelZoom(true);     //开启鼠标滚轮缩放
/*
var m_points = [];
var m_pointsnames = end_points_name;
let myGeo = new BMap.Geocoder();
for(let i = 0; i < m_pointsnames.length; i++){
    let pnames = m_pointsnames[i];
    myGeo.getPoint(pnames[0],
		   function(point){      
		       if (point) {      
			   console.log(point);
			   m_points[i] = point;
		       }      
		   }, 
		   pnames[1]);
}

//*/
